import java.util.Stack;

class Solution1 {
    public static int calculate(String s) {
        Stack<Integer> stack = new Stack<>();
        char prev = '+';
        int n = s.length();
        int tmp = 0;
        for (int i = 0; i < n; ++i) {
            //取出字符串中的每个字符
            char ch = s.charAt(i);
            //1.判断该字符是数字还是运算符
            //2.是数字的话进行数字处理判断
            //该方法判断传入字符是否为数字字符
            if (Character.isDigit(ch)){
                 tmp = tmp * 10 + (ch - '0');  //  + 123 + 2
            }
            if (!Character.isDigit(ch) && ch != ' ' || i == n - 1){
                switch (prev){
                    case '+':
                        stack.push(tmp);
                        break;
                    case '-':
                        stack.push(-tmp);
                        break;
                    case '*':
                        stack.push(stack.pop() * tmp);
                        break;
                    case '/':
                        stack.push(stack.pop() / tmp);
                        break;
                }
                //更新运算符
                prev = ch;
                //初始化tmp
                tmp = 0;
            }
        }
        int ret = 0;
        while (!stack.isEmpty()){
            ret += stack.pop();
        }
        return ret;
    }

    public static void main(String[] args) {
        System.out.println(Solution1.calculate("3+2*2"));
    }
}